﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using backoffice.Data;
using System.Linq.Expressions;

namespace backoffice.Business
{
    public class BusinessUser
    {
        #region Create
        public static void createUser(User u)
        {
            try
            {
                u.State = true;
                dataUser.createUser(u);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating user");
            }
        }

        public static void createUser(String email, String password, int permission, String name, String companyName,
                                      String address, String city, String postalcode, Region r, String phone,
                                      String usageTime, DateTime purchaseDate)
         {
            try
            {
                User u = new User();
                u.Email = email;
                u.Password = password;
                u.Permission = permission;
                u.Name = name;
                u.CompanyName = companyName;
                u.Address = address;
                u.City = city;
                u.PostalCode = postalcode;
                u.RegionID = r.RegionID;
                u.Phone = phone;
                u.PurchaseDate = purchaseDate;
                u.State = true;

                dataUser.createUser(u);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating user");
            }
        }
        #endregion

        #region Get
        public static User getUser(Guid guid)
        {
            try
            {
                return dataUser.getUser(guid);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting user");
            }
        }

        public static User getUserByUsername(String username)
        {
            try
            {
                Expression<Func<User, bool>> whereClause = (User u) => (u.Email.Equals(username));
                return dataUser.getUser(whereClause);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting user by username");
            }
        }
        #endregion

        #region Set
        public static Boolean changeUser(User u)
        {
            try
            {
                return dataUser.changeUser(u);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error changing user");
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteUser(User u)
        {
            try
            {
                return dataUser.deleteUser(u);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error deletting user");
            }
        }
        #endregion
    }
}
